import 'package:date_format/date_format.dart';
import 'package:flutter/cupertino.dart';
import 'package:imember/Util/MyColor.dart';
import 'package:imember/Util/PageUtil.dart';
import 'package:imember/Widget/MyButton.dart';
import 'package:imember/Widget/MyDialog.dart';
import 'package:imember/Widget/MyImage.dart';
import 'package:imember/Widget/MyInput.dart';
import 'package:imember/Widget/MyPage.dart';

import 'ScanReceive.dart';
import 'ShipmentDetail.dart';

class ShipmentManager extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ShipmentManager();
  }
}

class _ShipmentManager extends State {
  String startDate = "";
  String endDate = "";
  bool allCheck = false;

  printClick() {
    showBtn2Dialog(context, "是否列印出貨單", "取消", () {}, "確認", () {});
  }

  scanReceive() {
    showWidgetDialog(context, ScanReceive());
  }

  @override
  Widget build(BuildContext context) {
    return MyPage(
        Container(
            child: Column(children: [
          Row(mainAxisAlignment: MainAxisAlignment.center, children: [
            LocalImage(allCheck ? "ic_check_yes.png" : "ic_check_no.png", width: 23, height: 23, padding: EdgeInsets.fromLTRB(23, 0, 13, 0),
                onTap: () {
              setState(() {
                allCheck = !allCheck;
              });
            }),
            Text("全選", style: TextStyle(fontSize: 17, color: MyColor.C70)),
            Container(width: 11),
            Text("日期範圍", style: TextStyle(fontSize: 17, color: MyColor.C70)),
            Container(width: 19),
            CalendarChooser(startDate, onConfirm: (DateTime dateTime, List<int> selectedIndex) {
              setState(() {
                startDate = formatDate(dateTime, [yyyy, '-', mm, '-', dd]);
              });
            }),
            Container(height: 3, width: 12, color: MyColor.C70, margin: EdgeInsets.all(5)),
            CalendarChooser(endDate, onConfirm: (DateTime dateTime, List<int> selectedIndex) {
              setState(() {
                endDate = formatDate(dateTime, [yyyy, '-', mm, '-', dd]);
              });
            }),
            Expanded(child: Container()),
            Text("關鍵字搜尋", style: TextStyle(fontSize: 17, color: MyColor.C70)),
            Container(
              width: 189,
              height: 36,
              padding: EdgeInsets.fromLTRB(12, 0, 12, 0),
              margin: EdgeInsets.only(left: 8),
              decoration:
                  BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(24)), border: Border.all(width: 1.2, color: MyColor.C58)),
              child: NormalInput(),
            ),
            LocalImage("ic_search.png", width: 30, padding: EdgeInsets.all(13)),
            Container(width: 30),
          ]),
          Container(height: 2, color: MyColor.DD),
          Expanded(
            child: true
                ? Container(
                    child: Column(
                    children: [
                      Expanded(
                          child: ListView.separated(
                              padding: EdgeInsets.all(0),
                              itemCount: 4,
                              itemBuilder: (BuildContext context, int index) {
                                bool orderIng = index == 3;
                                return ItemOrder(orderIng: orderIng, check: allCheck ? true : false, onCheck: (isCheck) {});
                              },
                              separatorBuilder: (BuildContext context, int index) {
                                return Container(height: 2, color: MyColor.DD);
                              })),
                      Container(
                          padding: EdgeInsets.only(right: 20),
                          child: Row(mainAxisAlignment: MainAxisAlignment.end, children: [
                            OrangeBtn("掃描領貨", width: 170, height: 37, margin: EdgeInsets.all(10),onTap: scanReceive),
                            BlueBtn("列印", width: 170, height: 37, margin: EdgeInsets.all(10), onTap: printClick),
                          ]))
                    ],
                  ))
                : Center(child: Text("目前尚無紀錄", style: TextStyle(fontSize: 30, color: MyColor.C70, fontWeight: FontWeight.bold))),
          )
        ])),
        showNotice: true,
        showIcon: true);
  }
}

class ItemOrder extends StatefulWidget {
  bool? orderIng;
  Function(bool check) onCheck;
  bool check = false;

  ItemOrder({this.orderIng, required this.check, required this.onCheck});

  @override
  State<StatefulWidget> createState() {
    return _ItemOrder();
  }
}

class _ItemOrder extends State<ItemOrder> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        showWidgetDialog(context, ShipmentDetail());
      },
      behavior: HitTestBehavior.translucent,
      child: Container(
          padding: EdgeInsets.fromLTRB(0, 12, 0, 0),
          child: Row(children: [
            LocalImage(widget.check ? "ic_check_yes.png" : "ic_check_no.png",
                width: 23, height: 23, padding: EdgeInsets.fromLTRB(23, 0, 30, 0), onTap: () {
              setState(() {
                widget.check = !(widget.check);
              });
              widget.onCheck(widget.check);
            }),
            Text("箱數", style: TextStyle(fontSize: 16, color: MyColor.C51)),
            Container(width: 10),
            NumberBox(2, enable: widget.orderIng),
            Container(width: 32),
            Expanded(
                child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
              Text("\$53", style: TextStyle(fontSize: 20, color: MyColor.C51)),
              Row(children: [
                Text("訂單編號:", style: TextStyle(fontSize: 20, color: MyColor.C51)),
                Container(width: 120, child: Text("113834", style: TextStyle(fontSize: 20, color: MyColor.C99))),
                Text("包數:", style: TextStyle(fontSize: 20, color: MyColor.C51)),
                Container(width: 100, child: Text("50", style: TextStyle(fontSize: 20, color: MyColor.Orange))),
                Expanded(child: Text("出貨中", style: TextStyle(fontSize: 20, color: MyColor.Orange))),
                Text("2020/12/22 15:56", style: TextStyle(fontSize: 20, color: MyColor.C51)),
                LocalImage("ic_arrow_r.png", width: 48, height: 48),
              ])
            ]))
          ])),
    );
  }
}

class NumberBox extends StatelessWidget {
  int number;
  Function()? onSub;
  Function()? onAdd;
  bool? enable;

  NumberBox(this.number, {this.onSub, this.onAdd, this.enable});

  @override
  Widget build(BuildContext context) {
    enable = enable ?? true;
    return Row(children: [
      Container(
          height: 32,
          width: 32,
          decoration: BoxDecoration(border: Border.all(width: 1, color: MyColor.C70), color: enable! ? MyColor.Transfer : MyColor.C4),
          child: enable! ? LocalImage("ic_sub_g.png", width: 14, height: 14, padding: EdgeInsets.all(8), onTap: onSub) : Container()),
      Container(
          width: 40,
          height: 32,
          alignment: Alignment.center,
          decoration: BoxDecoration(
              color: enable! ? MyColor.Transfer : MyColor.C4,
              border: Border(top: BorderSide(width: 1, color: MyColor.C70), bottom: BorderSide(width: 1, color: MyColor.C70))),
          child: Text("$number", style: TextStyle(fontSize: 18))),
      Container(
          height: 32,
          width: 32,
          decoration: BoxDecoration(border: Border.all(width: 1, color: MyColor.C70), color: enable! ? MyColor.Transfer : MyColor.C4),
          child: enable! ? LocalImage("ic_add_g.png", width: 14, height: 14, padding: EdgeInsets.all(8), onTap: onAdd) : Container())
    ]);
  }
}
